//
//  CYBaseViewController.m
//  SafetyInHand
//
//  Created by admin on 2022/7/29.
//

#import "CYBaseViewController.h"
#import "CYBaseConst.h"
#import "UIImage+CYImageCategory.h"
#import "UIColor+ColorTemplate.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface CYBaseViewController () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *navBottomLineView;
@end

@implementation CYBaseViewController

- (BOOL)shouldAutorotate {
    return NO;
}

#pragma mark - Override Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    [self configNavigationView];
    __weak typeof(self) WS = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = WS;

    [self.view setBackgroundColor:[UIColor cy_EFEFF4]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = self.isHiddenNavigationBar;

    [self configNavBarColor];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 * 状态栏样式
 * @return
 */
- (UIStatusBarStyle)preferredStatusBarStyle {
    UIStatusBarStyle style = UIStatusBarStyleDefault;
    switch (self.navigationStatusBarColor) {
        case CYNavStatusBarColor_Black:
            if (@available(iOS 13.0, *)) {
                style = UIStatusBarStyleDarkContent;
            } else {
                style = UIStatusBarStyleDefault;
            }
            break;
        case CYNavStatusBarColor_White:
            style = UIStatusBarStyleLightContent;
            break;
    }
    return style;
}

#pragma mark - Navigation Bar

- (void)configNavigationView {
    [self configDefault];
    [self configNavBarTitleCSS];
    [self configNavBarColor];
}

- (void)configDefault {
    self.navigationBarColor = [UIColor whiteColor];
}

- (void)configNavBarTitleCSS {
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset = CGSizeMake(0, 0);
    shadow.shadowColor = [UIColor cy_323232];
    NSDictionary *css = @{
            NSForegroundColorAttributeName: [UIColor cy_323232],
            NSShadowAttributeName: shadow,
            NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};

    [self.navigationController.navigationBar setTitleTextAttributes:css];
}

/**
 * 重置导航栏颜色问题
 */

- (void)configNavBarColor {
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        [appearance configureWithOpaqueBackground];//重置导航栏背景颜色和阴影
        appearance.backgroundColor = self.navigationBarColor;
        appearance.shadowImage = [UIImage new];
        appearance.shadowColor = nil;
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    } else {
        UIImage *navBgImage = [UIImage cy_imageWithColor:self.navigationBarColor];

        // Fallback on earlier versions
        self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
        [self.navigationController.navigationBar setBackgroundImage:navBgImage forBarMetrics:UIBarMetricsDefault];
    }
}

#pragma mark - extern Methods

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
//    UIAlertView* alv=[[UIAlertView alloc] initWithTitle:@"内存告急" message:@"您的内存不足，请到设置里面清除缓存，或者检查手机上使用内存较大的app" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
//    [alv show];
}

#pragma mark - Methods

- (void)backDefaultAction:(UIButton *)button {
    NSArray *viewControllers = self.navigationController.viewControllers;
    if (viewControllers.count > 1) {
        if ([viewControllers objectAtIndex:viewControllers.count - 1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:!self.isCloseBackPopAnimated];
        }
    } else {
        //present方式
        [self.navigationController dismissViewControllerAnimated:!self.isCloseBackPopAnimated completion:nil];
    }
}

- (void)addNavigationTitle {
    self.navigationController.navigationBar.barTintColor = self.navigationBarColor;
    //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:0];
    self.navigationController.navigationBar.translucent = NO;
    if (_navigationTitle) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
        [titleLabel setText:_navigationTitle];
        [titleLabel setFont:[UIFont systemFontOfSize:IS_IPAD ? 19.0 : 18.0f]];
        [titleLabel setTextColor:_navigationTitleColor ?: RGBA_COLOR(50, 50, 50, 1)];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        self.navigationItem.titleView = titleLabel;
    }
}

#pragma mark - Lazy Property

- (void)setHiddenNavigationBar:(BOOL)hiddenNavigationBar {
    _hiddenNavigationBar = hiddenNavigationBar;
    self.fd_prefersNavigationBarHidden = _hiddenNavigationBar;
}

- (void)setDisableFullScreenPop:(BOOL)disableFullScreenPop {
    _disableFullScreenPop = disableFullScreenPop;
    self.fd_interactivePopDisabled = _disableFullScreenPop;
}

- (void)setNavigationTitle:(NSString *)navigationTitle {
    _navigationTitle = navigationTitle;

    UILabel *titleLabel = (UILabel *) self.navigationItem.titleView;
    if ([titleLabel isKindOfClass:[UILabel class]]) {
        [titleLabel setText:_navigationTitle];
    } else {
        [self addNavigationTitle];
    }
}

- (void)setNavLeftBtn:(UIButton *)navLeftBtn {
    _navLeftBtn = navLeftBtn;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_navLeftBtn];
}

- (void)setNavRightBtn:(UIButton *)navRightBtn {
    _navRightBtn = navRightBtn;

    self.navRightButtons = @[_navRightBtn];
}

- (void)setNavRightButtons:(NSArray<UIButton *> *)navRightButtons {
    _navRightButtons = navRightButtons;

    NSMutableArray *rightBars = [NSMutableArray array];
    NSInteger rightBtnCount = _navRightButtons.count;
    // 倒叙
    for (NSInteger i = rightBtnCount - 1; i >= 0; i--) {
        UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:_navRightButtons[i]];
        [rightBars addObject:btnItem];
    }
    self.navigationItem.rightBarButtonItems = rightBars;
}

- (void)setShowNavigationBarBottomLine:(BOOL)showNavigationBarBottomLine {
    _showNavigationBarBottomLine = showNavigationBarBottomLine;
    if (_showNavigationBarBottomLine) {
        [self.navigationController.navigationBar addSubview:self.navBottomLineView];
    }
}

- (void)setNavigationBarColor:(UIColor *)navigationBarColor {
    _navigationBarColor = navigationBarColor;
}

- (void)setNavigationStatusBarColor:(CYNavStatusBarColor)navigationStatusBarColor {
    _navigationStatusBarColor = navigationStatusBarColor;
    [self setNeedsStatusBarAppearanceUpdate];
}
@end